package com.asset.dispoal.file.document.service.command;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.file.document.domain.db.mapper.DocumentMapper;
import com.asset.dispoal.file.document.dto.DocumentPageRequest;
import com.asset.dispoal.file.document.dto.DocumentPageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

@Component
@Slf4j
public class DocumentQueryPageCommand implements BaseCommand<DocumentPageRequest, Result<PageVO<DocumentPageResponse>>> {

    @Autowired
    private DocumentMapper documentMapper;

    @Override
    public Result<PageVO<DocumentPageResponse>> execute(DocumentPageRequest request) {
        int count = documentMapper.queryPageCount(request);
        Page<DocumentPageRequest> page = PagingQuery.getPage(count, request);
        PageVO<DocumentPageResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<DocumentPageResponse> appointResponses = documentMapper.queryPage(page);
            pageVO.setResult(appointResponses);
        }
        return Result.ok(pageVO);
    }
}
